package com.gopay.common.cps.dao.wallet.appversioncontroller.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.appversioncontroller.WaAppVersionControllerDao;
import com.gopay.common.domain.dps.WaVersionController;

/**
 * 
 * @author dps
 *
 */
@Repository("waAppVersionControllerDao")
public class WaAppVersionControllerDaoImpl extends BaseDAOImpl<WaVersionController,Integer> implements WaAppVersionControllerDao {

	protected Logger logger = LoggerFactory.getLogger(WaAppVersionControllerDaoImpl.class);
	@Override
	public List<WaVersionController> getAppVersion(String appType,String appDevice,String appVersion,String startTime,String endTime){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuffer Hql = new StringBuffer("from WaVersionController t where 1=1 ");
		
		if (StringUtils.isNotBlank(appType)) {
			Hql.append("and t.appType = :appType ");
            paramMap.put("appType", appType );
        }
		if (StringUtils.isNotBlank(appDevice)) {
			Hql.append("and t.deviceType = :appDevice ");
            paramMap.put("appDevice", appDevice);
        }
		if (StringUtils.isNotBlank(appVersion)) {
			Hql.append("and t.appVersion = :appVersion ");
            paramMap.put("appVersion", appVersion);
        }
		if (StringUtils.isNotBlank(startTime)) {
			Hql.append("and t.createTime > to_date( :createTimeStart,'yyyy-mm-dd hh24:mi:ss') ");
            paramMap.put("createTimeStart", startTime);
        }
		if (StringUtils.isNotBlank(endTime)) {
			Hql.append("and t.createTime < to_date( :createTimeEnd,'yyyy-mm-dd hh24:mi:ss') ");
            paramMap.put("createTimeEnd", endTime);
        }
		
		Hql.append(" order by t.createTime desc");
		logger.info("查询hql语句"+Hql.toString());
		List<WaVersionController> waVersionControllerList = this.findByHql(Hql.toString(),paramMap);
		return waVersionControllerList;
	}
	
	@Override
	public WaVersionController getAppVersionById(Integer id){
		Map<String, Object> param = new HashMap<>();
        param.put("id", id);
		String hql = "from WaVersionController t where t.id = :id ";
		List<WaVersionController> waVersionControllerList = this.findByHql(hql,param);
		if( waVersionControllerList != null && waVersionControllerList.size() > 0 ){
			return waVersionControllerList.get(0);
		}
		return null;
	}
	
	/**
	 * 查询该设备类型应用类型的数据，降序排列
	 * @param deviceType 设备类型 android、ios
	 * @param appType 应用类型：1-个人版,2-商户版
	 * @return
	 */
	@Override
    public List<WaVersionController> findByAppTypeAndDeviceType(String deviceType, String appType){
		
		String hql = "from WaVersionController t where t.appType=:appType and t.deviceType=:deviceType ";
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("deviceType", deviceType);
		param.put("appType", appType);
		List<WaVersionController> waVersionControllerList = this.findByHql(hql,param);
		if( waVersionControllerList != null && waVersionControllerList.size() > 0 ){
			return waVersionControllerList;
		}
		return null;
	}
	
	
	/**
	 * 根据设备类型 、应用类型、版本号查询
	 * @param deviceType 设备类型 android:01、ios:02
	 * @param appType 应用类型：1-个人版,2-商户版
	 * @param appVersion
	 * @return
	 */
	@Override
    public WaVersionController findByAppVersion(String deviceType, String appType, String appVersion){
		
		String hql = "from WaVersionController t where t.appType=:appType and t.deviceType=:deviceType and t.appVersion=:appVersion";
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("deviceType", deviceType);
		param.put("appType", appType);
		param.put("appVersion", appVersion);
		List<WaVersionController> waVersionControllerList = this.findByHql(hql,param);
		if( waVersionControllerList != null && waVersionControllerList.size() > 0 ){
			return waVersionControllerList.get(0);
		}
		return null;
	}
	
}
